import worldMgr from "../map/worldMgr";

export class GlobalFunction
{
    //全屏widget
    static addWidgetCanvas(node){
        var _Widget = node.addComponent(cc.Widget);
        _Widget.target = cc.find("Canvas");
        _Widget.isAlignTop = true;
        _Widget.isAlignBottom = true;
        _Widget.isAlignLeft = true;
        _Widget.isAlignRight = true;
        _Widget.top = 0;
        _Widget.bottom = 0;
        _Widget.left = 0;
        _Widget.right = 0;
    }

    //异步测试
    static asyncTest(host:cc.Component){
        return new Promise((resolve, reject) => {
            setTimeout(() =>{
                if(cc.isValid(host))
                {
                    resolve();
                }
                else
                {
                    reject("异步主调不可用");
                }
            }, 5000)
        });
    }

    //异步加载预制体(可靠)
    static asyncLoadPrefabEx(Path:string, host:cc.Component){
        return new Promise<cc.Prefab>((resolve, reject) => {
            cc.loader.loadRes(Path, function (err, prefab) {
                if(err) {
                    reject("预制体读取错误 path:" + Path);
                }
                else{
                    if(cc.isValid(host)){
                        cc.loader.setAutoReleaseRecursively(prefab, true);
                        resolve(prefab);
                    }
                    else
                    {
                        reject("异步主调不可用");
                    }
                }
            });
        })
    }

    //异步加载预制体
    static asyncLoadPrefab(Path:string){
        return new Promise<cc.Prefab>((resolve, reject) => {
            cc.loader.loadRes(Path, function (err, prefab) {
                if(err) {
                    reject("预制体读取错误 path:" + Path);
                }
                else{
                    cc.loader.setAutoReleaseRecursively(prefab, true);
                    resolve(prefab);
                }
            });
        })
    }

    //异步预加载场景
    static asyncPreloadScene(sceneName:string, onProgress?:any){
        return new Promise<cc.SceneAsset>((resolve, reject) => {
            cc.director.preloadScene(sceneName,onProgress,function(err, asset:cc.SceneAsset){
                if(err) reject();
                resolve(asset);
            });
        });
    }

    //异步加载场景
    static asyncLoadScene(sceneName:string)
    {
        return new Promise((resolve, reject) => {
            var isSuc:boolean = cc.director.loadScene(sceneName, function(){
                resolve();
            });
            if(!isSuc) 
            {
                reject("场景加载失败");
            }
        })
    }

    //异步播放动画(可靠)
    static asyncPlayAnimationEx(Animation:cc.Animation, aniName:string, host:cc.Component)
    {
        return new Promise(function(resolve, reject){
            var aniState:cc.AnimationState = Animation.play(aniName);
            if(!aniState) {
                reject("动画播放失败");
            }
            else
            {
                aniState.on('stop', (event) => {
                    if(cc.isValid(host))
                        resolve();
                    else
                        reject("异步主调不可用");
                }, this);
            }
        })
    }

    //获取两点间距离
    static getTwoPointDis(p1:cc.Vec2, p2:cc.Vec2){
        return p1.sub(p2).mag();
    }

    //获取两点的向量
    static getTwoPointVec(p1:cc.Vec2, p2:cc.Vec2){
        return p1.sub(p2);
    }

    //四元数相乘
    static quatMul(q1:cc.Quat, q2:cc.Quat):cc.Quat{
        var quat = new cc.Quat(
            q1.w*q2.w - q1.x*q2.x - q1.y*q2.y - q1.z*q2.z,
            q1.w*q2.x + q1.x*q2.w + q1.z*q2.y - q1.y*q2.z,
            q1.w*q2.y + q1.y*q2.w + q1.x*q2.z - q1.z*q2.x,
            q1.w*q2.z + q1.z*q2.w + q1.y*q2.x - q1.x*q2.y,
        );
        return quat;
    }

    //向量角度
    static getVecDegree(dirVec:cc.Vec2 ,comVec:cc.Vec2):number{
        if(dirVec.equals(cc.v2(0,0))) return 0;
        let radian:number = dirVec.signAngle(comVec);    // 求方向向量与对比向量间的弧度
        let degree:number = cc.misc.radiansToDegrees(radian);    // 将弧度转换为角度
        return degree;
    }

    //获取方向
    //用于模型方向 取纵坐标正上为0
    static getDirection(dirVec:cc.Vec2):number{
        return -GlobalFunction.getVecDegree(dirVec, new cc.Vec2(0,-1));
    }

    //获取世界管理器
    static getWorldMgr():worldMgr{
        return <worldMgr>cc.find("Canvas/worldMgr").getComponent(cc.Component)
    }

    //地图节点在屏幕中的坐标
    static NodePosToUIPos(node:cc.Node){
        return this.MapPosToUIPos(node.convertToWorldSpaceAR(cc.v2(0,0)));
    }

    //地图坐标转屏幕坐标
    static MapPosToUIPos(mapPos:cc.Vec2) : cc.Vec2 {
        cc.log(mapPos);
        var pos = this.getWorldMgr().getCamara().getWorldToCameraPoint(mapPos, cc.v2(0,0));
        cc.log(pos);
        return pos;
    }

    //屏幕坐标转换为地图坐标
    static UIPosToMapPos(uiPos:cc.Vec2) : cc.Vec2{
        return this.getWorldMgr().getCamara().getCameraToWorldPoint(uiPos, cc.v2(0,0));
    }

    //UI坐标转换为地图节点坐标
    static UIPosToMapNodePos(node:cc.Node, uiPos:cc.Vec2) : cc.Vec2{
        return node.parent.convertToNodeSpaceAR(this.UIPosToMapPos(uiPos));
    }

    //节点截图
    static captureNode(node:cc.Node, texture:cc.RenderTexture){
        var capCamNode = cc.find("Canvas/capCam");
        if(!capCamNode)
        {
            capCamNode = new cc.Node("capCam");
            capCamNode.parent = cc.find("Canvas");
        }
        var capCam = capCamNode.getComponent(cc.Camera);
        if(!capCam){
            capCam = capCamNode.addComponent(cc.Camera);
        }
        capCamNode.active = true;
        capCamNode.setAnchorPoint(node.getAnchorPoint());
        capCamNode.setPosition(capCamNode.parent.convertToNodeSpaceAR(node.convertToWorldSpaceAR(cc.v2(0,0))));
        capCam.targetTexture = texture;
        capCam.render(node);
        capCam.targetTexture = null;
        capCamNode.active = false;
    }
}
